package com.atguigu.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableDemo1 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask1 = new FutureTask(new MyCallableImpl());
        FutureTask futureTask2 = new FutureTask(new MyCallableImpl());

        new Thread(futureTask1, "t1").start();
        System.out.println(futureTask1.get());
        new Thread(futureTask1, "t2").start();
        System.out.println(futureTask1.get());   //futureTask的缓存机制

        new Thread(futureTask2, "t3").start();
        System.out.println(futureTask1.get());   //new futureTask
//

        FutureTask<Integer> lambdaCall = new FutureTask<>(() -> {
            System.out.println("lambda 实现 callable");
            return 111;
        });
        new Thread(lambdaCall).start();
        System.out.println(lambdaCall.get());
    }


}


class MyCallableImpl implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {    //三个区别都在这一行了,返回值, 方法名字, 抛异常
        System.out.println("callable 执行中...");
        Thread.sleep(1000);
        return 2025;
    }
}